package cn.edu.neu.vmmigeval.cloud.policy;

import cn.edu.neu.vmmigeval.cloud.entity.DataCenter;
import cn.edu.neu.vmmigeval.cloud.entity.Host;
import cn.edu.neu.vmmigeval.cloud.entity.Vm;
import cn.edu.neu.vmmigeval.cloud.util.DatacenterUtil;
import cn.edu.neu.vmmigeval.io.ReadUtil;

import java.util.List;
import java.util.Map;

/**
 * File-based VM Deployment.
 *
 * @author yfwz100
 */
public class OneTimeFixMappingDeployment implements VMDeployment {

    private Map<Integer, List<Integer>> mapping;

    public OneTimeFixMappingDeployment(Map<Integer, List<Integer>> mapping) {
        this.mapping = mapping;
    }

    public static VMDeployment getFromResource(String name) {
        try {
            Map<Integer, List<Integer>> mapping = ReadUtil.readFromMappingFile(name);
            return new OneTimeFixMappingDeployment(mapping);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Host getHostForVm(Vm vm, DataCenter center) {
        Integer id = DatacenterUtil.getHostForVm(vm, mapping);
        if (id == null) {
            id = 0;
        }
        return center.getHosts().get(id);
    }
}
